package com.imooc.bigdata.hadoop.mr.project.Utils;/**
 * Created by luochangyong on 2019/8/15.
 */

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class LogParser {
    public Map<String, String> parse(String log){
        Map<String, String> info = new HashMap<>();
        if(StringUtils.isNotBlank(log)){
            String[] splits = log.split("\t");

            String ip = splits[1];
            String country = "-";
            String province = "-";
            String city = "-";
            IpParser.RegionInfo regionInfo = IpParser.analyseIp(ip);
            if(regionInfo != null){
                country = regionInfo.getCountry();
                province = regionInfo.getProvince();
                city = regionInfo.getCity();
            }
            info.put("ip", ip);
            info.put("country", country);
            info.put("province", province);
            info.put("city", city);

            String url = splits[0];
            info.put("url", url);

            String time = splits[2];
            info.put("time", time);
        }
        return info;
    }

    public Map<String, String> parseV2(String log){
        Map<String, String> info = new HashMap<>();
        if(StringUtils.isNotBlank(log)){
            String[] splits = log.split("\t");

            String ip = splits[0];
            String country = splits[1];
            String province = splits[2];
            String city = splits[3];

            info.put("ip", ip);
            info.put("country", country);
            info.put("province", province);
            info.put("city", city);

            String url = splits[4];
            info.put("url", url);

            String time = splits[5];
            info.put("time", time);

            String pageId = splits[6];
            info.put("pageId", pageId);
        }
        return info;
    }
}